package com.liuguangaop.aspect;

import com.alibaba.fastjson.JSONObject;
import com.liuguangaop.controller.TestAopController;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

/**
 * @ClassName: logAspectConfig
 * @Description：
 * @Author: wanghr
 * Date: 10:51 AM 2020/7/9
 * Version: 1.0
 */
@Aspect
@Component
@Slf4j
public class logAspectConfig {

    @Pointcut("execution(public * com.liuguangaop.controller..*Controller.*(..))")
    public void controllerPointcut() {

    }

    @Before("controllerPointcut()")
    public void before(JoinPoint joinPoint) {
        log.info("进入aop的 before方法");
//        log.info("在进入方法前对日志进行打印");
//        Object[] args = joinPoint.getArgs();
//
//        JSONObject arg = (JSONObject) args[0];
//        log.info("进入方法的参数：{}",arg );

    }

    @After(value = "controllerPointcut()")
    private void after(JoinPoint joinPoin) {
        log.info("进入AOP的 after 方法");

    }

    @AfterReturning(value = "controllerPointcut()",returning = "r")
    public Object afterReturning(JoinPoint joinPoin,Object r) {
        log.info("进入AOP 的 AfterReturning方法");
        r = r + "2222222";
        log.info("获取到方法执行的结果：{}",r);
        return r;
    }

    @AfterThrowing("controllerPointcut()")
    public void afterThrowing() {
        log.info("进入AOP 的 afterThrowing方法");
    }

//    @Around("controllerPointcut()")
//    public void around(ProceedingJoinPoint joinPoint) {
//        log.info("进入around方法");
//        Object[] args = joinPoint.getArgs();
//        Signature signature = joinPoint.getSignature();
//        try {
//            Object proceed = joinPoint.proceed();
//        } catch (Throwable throwable) {
//            throwable.printStackTrace();
//        }
//        log.info("离开around方法");
//    }
}
